<template>
  <div class="register">
    <div class="box">
      <div class="left">
        <img src="../assets/7.png" alt="" />
      </div>
      <div class="right">
        <div>
          <h2>访客注册</h2>
          <div>
            <a-form
              ref="formRef"
              :model="formState"
              :rules="rules"
              :label-col="labelCol"
              :wrapper-col="wrapperCol"
            >
              <a-form-item label="账户" name="name">
                <a-input
                  v-model:value="formState.name"
                  placeholder="请输入用户名"
                />
              </a-form-item>

              <a-form-item label="密码" name="password">
                <a-input-password
                  v-model:value="formState.password"
                  placeholder="请输入密码"
                />
              </a-form-item>

              <a-form-item label="确认" name="passwords">
                <a-input-password
                  v-model:value="formState.passwords"
                  placeholder="请再次输入密码"
                />
              </a-form-item>

              <a-form-item :wrapper-col="{ offset: 2 }">
                <a-button type="primary" @click="onSubmit">注册</a-button>
              </a-form-item>
            </a-form>
          </div>
          <div style="padding-left: 7%; cursor: pointer">
            <span @click="jump" style="color: #000">Or 去登录</span>
          </div>
        </div>
      </div>
    </div>
    <div class="login-jump" v-if="state.flag">
      <div class="x-box">
        <div style="padding-left: 30px">
          <li style="display: flex; align-items: center; padding-top: 25px">
            <ExclamationCircleOutlined style="color: orange; font-size: 20px" />
            <span style="padding-left: 10px">注册成功</span>
          </li>
          <p style="padding-left: 30px; padding-top: 5px">是否跳转至登录？</p>
          <div style="display: flex; justify-content: flex-end">
            <li>
              <a-button
                @click="
                  () => {
                    state.flag = false
                  }
                "
              >
                取消
              </a-button>
              <a-button type="primary" style="margin: 10px" @click="jump">
                确定
              </a-button>
            </li>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import axios from 'axios'
import { defineComponent, reactive, ref, toRaw } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
export default defineComponent({
  components: {
    ExclamationCircleOutlined
  },
  setup() {
    let state = reactive({
      flag: false
    })
    let router = useRouter()
    let jump = () => {
      router.push('/login')
    }
    const formRef = ref()
    const formState = reactive({
      name: undefined,
      password: undefined,
      passwords: undefined
    })
    const rules = {
      name: {
        required: true,
        message: '请输入用户名！'
      },
      password: {
        required: true,
        message: '请输入密码！'
      },
      passwords: {
        required: true,
        message: '请再次输入密码！'
      }
    }
    const onSubmit = () => {
      formRef.value
        .validate()
        .then(() => {
          console.log('values', formState)
          let data = {
            username: formState.name,
            password: formState.passwords
          }
          if (formState.password !== formState.passwords) {
            message.error('两次密码不统一')
          } else {
            axios
              .post('http://localhost:7001/api/register', data)
              .then((res) => {
                if (res.data.status == 200) {
                  state.flag = true
                } else {
                  message.error(res.data.message)
                }
              })
          }
        })
        .catch((error: any) => {
          console.log('error', error)
        })
    }
    return {
      state,
      formRef,
      labelCol: { span: 3 },
      wrapperCol: { span: 19 },
      formState,
      rules,
      onSubmit,
      jump
    }
  }
})
</script>

<style lang="scss">
.register {
  width: 100vw;
  height: 100vh;
  background: #e7eaee;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 80%;
    height: 85%;
    background: #fff;
    display: flex;
    .left {
      width: 50%;
      height: 100%;
      img {
        width: 100%;
        -webkit-user-drag: none;
      }
    }
    .right {
      width: 50%;
      height: 100%;
      background: #f7fafb;
      padding-top: 11%;
      h2 {
        padding-left: 20px;
      }
      button {
        width: 91%;
        margin-top: 5px;
      }
      .anticon svg {
        display: none;
      }
    }
  }
  .login-jump {
    width: 100vw;
    height: 100vh;
    position: absolute;
    top: 0;
    background: rgba(0, 0, 0, 0.4);
    display: flex;
    justify-content: center;
    .x-box {
      width: 400px;
      height: 150px;
      background: #fff;
      margin-top: 90px;
      border-radius: 5px;
    }
  }
}
</style>
